package com.code2roc.fastface.controller;

import com.code2roc.fastface.bizlogic.FaceRegistLogic;
import com.code2roc.fastface.model.FaceRegistDO;
import com.code2roc.fastface.model.RegistResult;
import com.code2roc.fastface.model.Result;
import com.code2roc.fastface.service.IFaceRegistService;
import com.code2roc.fastface.util.CommonUtil;
import com.code2roc.fastface.util.ConvertOp;
import com.code2roc.fastface.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/face/regist")
public class FaceRegistController {
    @Autowired
    private IFaceRegistService faceRegistService;
    @Autowired
    private FaceRegistLogic faceRegistLogic;

    //用于初始化人脸库测试使用
    @ResponseBody
    @PostMapping("/init")
    public Object init(@RequestBody Map<String, Object> params) {
        Result result = Result.okResult();
        File imageDir = new File("I:\\face\\img_align_celeba");
        for (File file : imageDir.listFiles()) {
            RegistResult registResult = faceRegistLogic.addFaceV2(CommonUtil.getNewGuid(), file.getName(), file.getAbsolutePath());
            if (registResult.getStatus() == 0) {
                System.out.println("成功注册人脸：" + file.getName());
            }
        }
        return result;
    }

    @ResponseBody
    @PostMapping("/list")
    public Object list(@RequestBody Map<String, Object> params) {
        Result result = Result.okResult();
        HashMap<String, Object> paramMap = new HashMap<>();
        String sql = "1=1";
        String userName = ConvertOp.convert2String(params.get("userName"));
        if (!StringUtil.isEmpty(userName)) {
            sql += " and userName like #{userName}";
            paramMap.put("userName", "%" + userName + "%");
        }
        String userID = ConvertOp.convert2String(params.get("userID"));
        if (!StringUtil.isEmpty(userID)) {
            sql += " and userID like #{userID}";
            paramMap.put("userID", "%" + userID + "%");
        }
        List<FaceRegistDO> rows = faceRegistService.selectPageList("userID,userName,registDate,updateDate", sql, "id desc", paramMap);
        int count = faceRegistService.selectCount(sql, paramMap);
        result.add("rows", rows);
        result.add("total", count);
        return result;
    }

    @ResponseBody
    @PostMapping("/insert")
    public Object insert(@RequestBody Map<String, Object> params) {
        Result result = Result.okResult();
        String userID = ConvertOp.convert2String(params.get("userID"));
        String userName = ConvertOp.convert2String(params.get("userName"));
        String base64Image = ConvertOp.convert2String(params.get("base64Image"));
        if (faceRegistService.checkExist("userID", userID)) {
            return Result.errorResult().setMsg("用户ID已注册");
        }
        RegistResult registResult = faceRegistLogic.addFace(userID, userName, base64Image);
        if (registResult.getStatus() != 0) {
            return Result.errorResult().setMsg(registResult.getMessage());
        }
        return result;
    }

    @ResponseBody
    @PostMapping("/update")
    public Object update(@RequestBody Map<String, Object> params) {
        Result result = Result.okResult();
        String userID = ConvertOp.convert2String(params.get("userID"));
        String base64Image = ConvertOp.convert2String(params.get("base64Image"));
        if (!faceRegistService.checkExist("userID", userID)) {
            return Result.errorResult().setMsg("用户ID不存在");
        }
        RegistResult registResult = faceRegistLogic.updateFace(userID, base64Image);
        if (registResult.getStatus() != 0) {
            return Result.errorResult().setMsg(registResult.getMessage());
        }
        return result;
    }

    @ResponseBody
    @PostMapping("/updateInfo")
    public Object updateInfo(@RequestBody Map<String, Object> params) {
        Result result = Result.okResult();
        String userID = ConvertOp.convert2String(params.get("userID"));
        String userName = ConvertOp.convert2String(params.get("userName"));
        FaceRegistDO faceRegistDO = faceRegistService.selectOne(userID);
        faceRegistDO.setUserName(userName);
        faceRegistService.update(faceRegistDO);
        return result;
    }

    @ResponseBody
    @PostMapping("/delete")
    public Object delete(@RequestBody Map<String, Object> params) {
        Result result = Result.okResult();
        String userID = ConvertOp.convert2String(params.get("userID"));
        if (!faceRegistService.checkExist("userID", userID)) {
            return Result.errorResult().setMsg("用户ID不存在");
        }
        RegistResult registResult = faceRegistLogic.deleteFace(userID);
        if (registResult.getStatus() != 0) {
            return Result.errorResult().setMsg(registResult.getMessage());
        }
        return result;
    }

    @ResponseBody
    @PostMapping("/detail")
    public Object detail(@RequestBody Map<String, Object> params) {
        Result result = Result.okResult();
        String userID = ConvertOp.convert2String(params.get("userID"));
        FaceRegistDO faceRegistDO = faceRegistService.selectOne(userID);
        result.add("obj", faceRegistDO);
        return result;
    }

    @ResponseBody
    @PostMapping("/query")
    public Object query(@RequestBody Map<String, Object> params) {
        Result result = Result.okResult();
        String base64Image = ConvertOp.convert2String(params.get("base64Image"));
        RegistResult registResult = faceRegistLogic.queryFace(base64Image);
        if (registResult.getStatus() != 0) {
            return Result.errorResult().setMsg(registResult.getMessage());
        }
        FaceRegistDO faceRegistDO = faceRegistService.selectOneByField("registIndex", registResult.getMessage());
        result.add("userID", faceRegistDO.getUserID());
        result.add("userName", faceRegistDO.getUserName());
        return result;
    }

    @ResponseBody
    @PostMapping("/resetFaceDBIndex")
    @Transactional(rollbackFor = {Exception.class}, isolation = Isolation.READ_UNCOMMITTED)
    public Object resetFaceDBIndex(@RequestBody Map<String, Object> params) throws Exception {
        Result result = Result.okResult();
        faceRegistLogic.resetFaceDBIndex();
        return result;
    }

    @ResponseBody
    @PostMapping("/compare")
    public Object compare(@RequestBody Map<String, Object> params) {
        Result result = Result.okResult();
        String sourceBase64Image = ConvertOp.convert2String(params.get("sourceBase64Image"));
        String compareBase64Image = ConvertOp.convert2String(params.get("compareBase64Image"));
        RegistResult registResult = faceRegistLogic.compareFace(sourceBase64Image, compareBase64Image);
        if (registResult.getStatus() != 0) {
            return Result.errorResult().setMsg(registResult.getMessage());
        } else {
            result.setMsg(registResult.getMessage());
        }
        return result;
    }

    @ResponseBody
    @PostMapping("/check")
    public Object check(@RequestBody Map<String, Object> params) {
        Result result = Result.okResult();
        String base64Image = ConvertOp.convert2String(params.get("base64Image"));
        RegistResult registResult = faceRegistLogic.checkFace(base64Image);
        if (registResult.getStatus() != 0) {
            return Result.errorResult().setMsg(registResult.getMessage());
        } else {
            result.setMsg(registResult.getMessage());
        }
        return result;
    }
}
